package com.tigercel.xiangchengyun.adapter

import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import cn.jzvd.JZVideoPlayerStandard
import com.bumptech.glide.Glide
import com.tigercel.xiangchengyun.R
import com.tigercel.xiangchengyun.entity.NewsBean
import com.tigercel.xiangchengyun.utils.Global
import kotlinx.android.synthetic.main.item_video_special.view.*
import java.text.SimpleDateFormat
import java.util.*


class VideoSpecialAdapter(private val news: MutableList<NewsBean>,
                          private val itemClick: (NewsBean) -> Unit) :
        RecyclerView.Adapter<VideoSpecialAdapter.ViewHolder>() {
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder? {
        val view = LayoutInflater.from(parent.context)
                .inflate(R.layout.item_video_special, parent, false)
        return ViewHolder(view, itemClick)
    }

    override fun getItemCount(): Int = news.size

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        holder.bindNews(news[position])

    }

    class ViewHolder(view: View, private val itemClick: (NewsBean) -> Unit) :
            RecyclerView.ViewHolder(view) {
        fun bindNews(newsBean: NewsBean) {
            with(newsBean) {
                val sdf = SimpleDateFormat("yyyy-MM-dd")
                var d: String? = null
                try {
                    d = sdf.format(Date(pub_time.toLong() * 1000L))
                } catch (e: Exception) {
                }
                itemView.player.setAllControlsVisiblity(View.GONE, View.GONE, View.GONE, View.GONE, View.GONE, View.GONE, View.GONE)
                itemView.player.setUp(url, JZVideoPlayerStandard.SCREEN_WINDOW_NORMAL, "")
                Glide.with(Global.context).load(thumb_img).into(itemView.player.thumbImageView)
                itemView.title.text = title
                itemView.time.text = d
                itemView.tv_look_up.text = "$lookUp"
                itemView.news_source.text = author
                itemView.setOnClickListener { itemClick(this) }
            }
        }

    }

    fun addAll(data: MutableList<NewsBean>) {
        news.addAll(data)
        notifyDataSetChanged()
    }

}